#include "laig2plog.h"
#include "../src/boards/BoardModelling.h"

extern char game_board[BOARD_DIMENSION][BOARD_DIMENSION];

char* handleCommand(SOCKET *m_socket, char *initCommand, char board[BOARD_DIMENSION][BOARD_DIMENSION], char *player) {

	char *resp;
	char *boardArray = vectorToChar(board, BOARD_DIMENSION);
	char *command = createCommand(initCommand, boardArray, player);
	
	resp = exchange(m_socket, command);

	// heap corruption here
	// delete[] boardArray;
	delete[] command;
	return resp;
}

char* checkEnd(SOCKET *m_socket, char board[BOARD_DIMENSION][BOARD_DIMENSION], char *player) {

	char *resp = handleCommand(m_socket, "check_end", board, player);
	return resp;
}

char* executePlay(SOCKET *m_socket, char board[BOARD_DIMENSION][BOARD_DIMENSION], char *MovementAndPlayer) {

	char *resp = handleCommand(m_socket, "execute", board, MovementAndPlayer);
	return resp;
}

char* createCommand(char *command, char *arg1, char *arg2) {

	char *fullCommand = new char[strlen(command) + strlen(arg1) + strlen(arg2) + 6];
	
	strcpy(fullCommand, command);

	strcat(fullCommand, "(");
	strcat(fullCommand, arg1);
	strcat(fullCommand, ",");
	strcat(fullCommand, arg2);
	strcat(fullCommand, ").\n");

	return fullCommand;
}

// Calculates the necessary bytes to hold a board of dimension boardDimension,
// represented as a Prolog list of lists.
int arraySize(int x) {
	return (2 + x - 1 + x*2 + (x-1)*x + x*x);
}

char* vectorToChar(char board[BOARD_DIMENSION][BOARD_DIMENSION], int boardDimension) {

	int totalBytes = arraySize(boardDimension);
	char *arrayBoard = new char[totalBytes];

	memset(arrayBoard, '0', totalBytes);

	strcpy(arrayBoard, "[");
	
	for(int i=0; i < boardDimension; i++) {
		strcat(arrayBoard, "[");

		for(int j=0; j < boardDimension; j++) {
			
			char src[2];
			src[0] = board[i][j];
			src[1] = '\0';
			strcat(arrayBoard, src);
			if (j+1 != boardDimension)
				strcat(arrayBoard, ",");
		}

		strcat(arrayBoard, "]");
		if (i+1 != boardDimension)
			strcat(arrayBoard, ",");
	}
	strcat(arrayBoard, "]");
	return arrayBoard;
}

bool prologCharToVector(char *board) {

	int real_i = 0;
	for(unsigned i = 0; i < strlen(board); i++) {
		if (board[i] != '[' && 
			board[i] != ']' &&
			board[i] != ',' &&
			board[i] != '.' &&
			board[i] != '\0') {
				int y = real_i / BOARD_DIMENSION;
				int x = real_i % BOARD_DIMENSION;
				//printf("i:%d y:%d x:%d\n",real_i,y,x);
				game_board[y][x] = board[i];
				real_i++;	
				//result.push_back(board[i]);
		}
	}
	return true;
}

char* createPlayCommand(int y0, int x0, int y1, int x1, int player) {

	char numbers[4];
	char* response = new char[8];
	strcpy(response, "'");
	//puts("playcmd");

	itoa(x0, numbers, 10);
	strcat(response, numbers);

	itoa(y0, numbers, 10);
	strcat(response, numbers);
	
	itoa(x1, numbers, 10);
	strcat(response, numbers);

	itoa(y1, numbers, 10);
	strcat(response, numbers);
		
	//puts("playcmd2");
	strcat(response, "',");
	itoa(player, numbers, 10);
	strcat(response, numbers);
	strcat(response, "\0");

	return response;
}